/*
 * Copyright 2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package be.free.ts.support;

import com.google.common.collect.ComparisonChain;
import com.google.common.collect.Ordering;
import com.google.common.collect.Range;
import org.eclipse.persistence.annotations.Convert;
import org.joda.time.DateTime;

import javax.annotation.Nullable;
import javax.persistence.Embeddable;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import java.util.Date;

/**
 * @author Christophe De Blende
 * @since 09/12/12
 */
@Embeddable
public class Interval implements Comparable<Interval> {

	@Temporal(TemporalType.TIMESTAMP)
	@Convert("DateTimeConverter")
	private DateTime startDateTime;

	@Temporal(TemporalType.TIMESTAMP)
	@Convert("DateTimeConverter")
	private DateTime endDateTime;

	@Nullable
	public DateTime getStartDateTime() {
		return startDateTime;
	}

	@Nullable
	public DateTime getEndDateTime() {
		return endDateTime;
	}

	@Nullable
	public Date getStartDate() {
		if (startDateTime == null) {
			return null;
		}
		return startDateTime.toDate();
	}

	@Nullable
	public Date getEndDate() {
		if (endDateTime == null) {
			return null;
		}
		return endDateTime.toDate();
	}

	public void setStartDateTime(@Nullable DateTime startDateTime) {
		this.startDateTime = startDateTime;
	}

	public void setEndDateTime(@Nullable DateTime endDateTime) {
		this.endDateTime = endDateTime;
	}

	public void setStartDate(@Nullable Date startDate) {
		this.startDateTime = startDate == null ? null : new DateTime(startDate);
	}

	public void setEndDate(@Nullable Date endDate) {
		this.endDateTime = endDate == null ? null : new DateTime(endDate);
	}

	private static Interval fromRange(Range<DateTime> dateTimeRange) {
		Interval interval = new Interval();
		interval.setStartDateTime(dateTimeRange.lowerEndpoint());
		interval.setEndDateTime(dateTimeRange.upperEndpoint());
		return interval;
	}

	private Range<DateTime> toRange() {
		if (startDateTime == null && endDateTime == null) {
			return Range.all();
		}

		if (startDateTime == null) {
			return Range.atMost(endDateTime);
		}

		if (endDateTime == null) {
			return Range.atLeast(startDateTime);
		}

		return Range.closed(startDateTime, endDateTime);
	}

	@Override
	public int compareTo(Interval o) {
		return ComparisonChain.start()
				.compare(startDateTime, o.startDateTime, Ordering.natural().nullsFirst())
				.compare(endDateTime, o.endDateTime, Ordering.natural().nullsLast())
				.result();
	}

	public boolean isConnected(Interval interval) {
		return toRange().isConnected(interval.toRange());
	}

	public Interval span(Interval interval) {
		return fromRange(toRange().span(interval.toRange()));
	}

	public boolean contains(DateTime dateTime) {
		return toRange().contains(dateTime);
	}

	public boolean containsAll(Iterable<? extends DateTime> dateTimes) {
		return toRange().containsAll(dateTimes);
	}

	public boolean encloses(Interval interval) {
		return toRange().encloses(interval.toRange());
	}

	public Interval intersection(Interval interval) {
		return fromRange(toRange().intersection(interval.toRange()));
	}

}
